package com.laoda.ikun_food.controller.web.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoda.ikun_food.entity.Food;
import com.laoda.ikun_food.service.FoodService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@AllArgsConstructor
public class FoodRestController {

    private final FoodService foodService;

    @GetMapping("/api/foods")
    public IPage<Food> getAllSights(
            @RequestParam(name="searchFoodName",defaultValue = "", required = false) String searchFoodName,
            @RequestParam(name="lowPrice",defaultValue = "0", required = false) BigDecimal lowPrice,
            @RequestParam(name="hiPrice",defaultValue = "0", required = false) BigDecimal hiPrice,
            @RequestParam(name = "page", defaultValue = "1", required = false) int page,
            @RequestParam(name = "size", defaultValue = "10", required = false) int size
    ){
        IPage<Food> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(size);

        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        if(searchFoodName != null && !searchFoodName.isEmpty()){
            wrapper.like("name", searchFoodName);
        }
        if(lowPrice != null && !lowPrice.equals(BigDecimal.ZERO)){
            wrapper.ge("price", lowPrice);
        }
        if(hiPrice != null && !hiPrice.equals(BigDecimal.ZERO)){
            wrapper.le("price", hiPrice);
        }
        return foodService.page(pager, wrapper);
    }
}
